/******************************************************************************
@file:  Notification.java
@author Ramesh Anandhan

DESCRIPTION
-----------------------------------------------------------------------------
Copyright (c) 2011, GEEKWAVE SOLUTIONS INC.

All rights reserved.
******************************************************************************/

package com.smartc.notification;

import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
//import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.smartc.connection.HttpConnection;
import com.smartc.ui.AccountLogin;
import com.smartc.ui.R;

public class Notification extends Activity {
	protected static final String TAG = "Notification";
	private ArrayList<Object> itemList;
	private ItemBean bean;

	ListView mItemListView;
	ListViewAdapter adapter;
	private HttpConnection mConnection;
	private ResponseHandler mResponse = new ResponseHandler();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.notification);
		mConnection = new HttpConnection();
		mConnection.AddHandler(mResponse);
	}

	@Override
	protected void onStart() {
		super.onStart();
		String domain = getResources().getString(R.string.domain);
		String query = getResources().getString(R.string.url_notification);
		String url = domain + query;
		Log.i(TAG, "onStart url:" + url);
		mConnection.setUrl(url);
		mConnection.clearParam();
		mConnection.Execute();
	}

	public class ResponseHandler extends DefaultHandler {

		private boolean notification = false;

		public ArrayList<String> Notification_list = new ArrayList<String>();

		@Override
		public void characters(char[] ch, int start, int length)
				throws SAXException {
			if (this.notification) {
				Notification_list.add(new String(ch, start, length));
			}
		}

		@Override
		public void endElement(String uri, String localName, String qName)
				throws SAXException {
			if (localName.equals("a")) {
				this.notification = false;
			} else if (localName.equals("b")) {
				this.notification = false;
			} else if (localName.equals("Notifications")) {
				Message myMessage = new Message();
				myMessage.obj = "NOTIFICATION";
				handler.sendMessage(myMessage);
			}
		}

		@Override
		public void startElement(String uri, String localName, String qName,
				Attributes attributes) throws SAXException {
			if (localName.equals("a")) {
				this.notification = true;
			} else if (localName.equals("b")) {
				this.notification = true;
			} else if (localName.equals("Notifications")) {
				Notification_list.clear();
			}
		}
	}

	public static class ViewHolder {
		TextView ItemNot;
	}

	private class ListViewAdapter extends BaseAdapter {

		private LayoutInflater inflater;
		// private Context mContext;
		ArrayList<Object> mItemList;

		ListViewAdapter(Context context, ArrayList<Object> itemList) {
			super();
			// mContext = context;
			mItemList = itemList;
			this.inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}

		@Override
		public int getCount() {
			return mItemList.size();
		}

		@Override
		public Object getItem(int position) {
			return position;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {

			ViewHolder holder;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = inflater.inflate(R.layout.notification_list, null);

				holder.ItemNot = (TextView) convertView
						.findViewById(R.id.notification_desc);

				convertView.setTag(holder);
			} else
				holder = (ViewHolder) convertView.getTag();

			ItemBean bean = (ItemBean) itemList.get(position);

			holder.ItemNot.setText(bean.getItemNot());
			return convertView;
		}
	}

	public class ItemBean {
		String mItemNot;

		public String getItemNot() {
			return mItemNot;
		}

		public void setItemNot(String noti) {
			this.mItemNot = noti;
		}

	}

	public void AddObjectToList(String desc) {

		bean = new ItemBean();
		bean.setItemNot(desc);

		itemList.add(bean);
	}

	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			if (AccountLogin.DEBUG)
				Log.i(TAG, "handleMessage msg:" + (String) msg.obj);
			String loginmsg = (String) msg.obj;
			if (loginmsg.equals("NOTIFICATION")) {
				if (AccountLogin.DEBUG)
					Log.i(TAG, "handleMessage noti count"
							+ mResponse.Notification_list.size());
				itemList = new ArrayList<Object>();
				for (int i = 0; i < mResponse.Notification_list.size(); i++) {
					AddObjectToList(mResponse.Notification_list.get(i));
				}

				mItemListView = (ListView) findViewById(R.id.notification_list);
				adapter = new ListViewAdapter(Notification.this
						.getApplicationContext(), itemList);
				mItemListView.setAdapter(adapter);
			}
		}
	};

}
